package com.yds.ydsdatasetbackend.manager.rabbitmq.info;

import com.rabbitmq.client.Channel;

import com.yds.ydsdatasetbackend.file.info.FileRemoteService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class MyFileMessageConsumer {

    @Resource
    private FileRemoteService fileRemoteService;

    // 指定程序监听的消息队列和确认机制
    @SneakyThrows
    @RabbitListener(queues = {"file_message_queue"}, ackMode = "MANUAL") // 手动确认
    public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        log.info("receiveMessage message = {}", message);
        long fileId = Long.parseLong(message);
        try {
//            judgeService.doJudge(questionSubmitId);
//            // todo 测试
//            File file = fileService.getById(fileId);
//            if(file == null) {
//                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "文件不存在");
//            }
//            file.setIntroduction("测试");
//            file.setFillStatus(FileFillEnum.SUCCEED.getValue());
//            fileService.updateById(file);
            fileRemoteService.getInfo(fileId);


            channel.basicAck(deliveryTag, false);
        }catch (Exception e){
            channel.basicNack(deliveryTag, false, false);
        }
    }
}
